import Mock from 'mockjs'
import * as user from './modules/user'
import * as menu from './modules/menu'
import * as login from './modules/login'
import * as serverList from './modules/serverList'
import * as vpnuserList from './modules/vpnuserList'

// 设置全局延时
Mock.setup({
  timeout: '300-600'
})

// 1. 开启/关闭[业务模块]拦截, 通过调用fnCreate方法[isOpen参数]设置.
// 2. 开启/关闭[业务模块中某个请求]拦截, 通过函数返回对象中的[isOpen属性]设置.
// fnCreate(login, true)
fnCreate(user, true)
fnCreate(menu, true)
fnCreate(login, true)
fnCreate(serverList, true)
fnCreate(vpnuserList, true)


/**
 * 创建mock模拟数据
 * @param {*} mod 模块
 * @param {*} isOpen 是否开启?
 */
function fnCreate (mod, isOpen = true) {
  if (isOpen) {
    for (const key in mod) {
      ((res) => {
        if (res.isOpen !== false) {
          Mock.mock(new RegExp(res.url), res.type, (opts) => {
            opts.data = opts.body ? JSON.parse(opts.body) : null
            delete opts.body
            console.log('\n')
            console.log('%cmock拦截, 请求: ', 'color:blue', opts)
            console.log('%cmock拦截, 响应: ', 'color:blue', res.data)
            return res.data
          })
        }
      })(mod[key]() || {})
    }
  }
}


// Mock.mock('http://localhost:10000/user', {
//   name: '@name',
//   email: '@emaol',
//   'age|1-10': 5
// })
//
// Mock.mock('http://localhost:10000/menu', {
//   id: '@increment',
//   name: 'menu',
//   'order|10-20': 12
// })
